<?php
namespace app\api\controller;


use app\extra\DBCont;
use think\Db;
use function EasyWeChat\Kernel\data_get;

/**
 * @title 创建团队赛
 * @description 接口说明
 * @group 微信报名分销
 * @header name:authorization require:1 default: desc:登陆的token
 */
class Team extends Base
{
    protected $_order = 'id desc';
    /**
     * @title 获取
     * @description 接口说明
     * @author hardy
     * @url /api/Team
     * @method GET
     *
     * @param name:id type:int require:0 default:1 other: desc:唯一ID，获取某条记录详情，则为必填，id和title选填其一，默认title优先
     * @param name:title type:string require:0 default:1 other: desc:唯一ID，获取某条记录详情，则为必填，id和title选填其一
     * @param name:match_id type:int require:0 default:1 other: desc:查询团队是否存在，传title参数时必填
     *
     * @return team_name:团队名称
     * @return name:姓名
     * @return image:照片
     * @return sex:性别：男、女
     * @return age:年龄
     * @return tel:手机号
     * @return card:身份证号
     * @return unit:工作单位
     * @return province:省
     * @return city:市
     * @return area:区
     * @return address:详细地址
     * @return status:审核状态：0待审核，1通过审核，2拒绝
     * @return examine1:问题1：是、否
     * @return examine2:问题2：是、否
     * @return examine3:问题3：是、否
     * @return examine4:问题4：离开原因、否
     * @return accident_insurance:是否接受大赛期间意外伤害保险：是、否
     *
     * @return list:消息列表,有page参数则返回值list，list是一个数组，
     * @return size:每页最大数，默认20，可由前端传递参数决定 page参数返回值，
     * @return page:当前页，page参数返回值，
     * @return count:总记录数， page参数返回值，
     */
    public function gets()
    {
        $data = input("get.");
        if (!empty($data['title'])){
            $title =  (string)str_replace(' ', '', (string)$data['title']);
            $data = model('Team')->where('title', $title)->where('match_id', $data['match_id'])->find();
            $this->responseSuccess($data);
        } elseif (!empty($data['id'])){
            $id =  (int)str_replace(' ', '', (string)$data['id']);
            $data = model('Team')->where('id', $id)->find();
            $this->responseSuccess($data);
        } else {
            $map = [];
            $this->_searchPage($map);
            $this->responsePage($map, $this->_order, $this->controller);
        }
    }
    protected function _searchPage(&$where)
    {
        $data = input('get.');
        $key = 'title';
        if(!empty($data[$key])){
            $where[] = [$key,'like', "%{$data[$key]}%"];
        }
    }
    /**
     * @title 创建团队赛
     * @description 创建
     * @author hardy
     * @url /api/Team
     * @method POST
     *
     * @param name:match_id type:int require:0 default: other: desc:赛事id，团队不存在时必填，（团队存在即：team_id有值）
     * @param name:cate_id type:int require:0 default: other: desc:赛事分类id，团队不存在时必填，（团队存在即：team_id有值）
     * @param name:cover type:int require:0 default: other: desc:团队照片，团队不存在时必填，（团队存在即：team_id有值）
     * @param name:title type:string require:0 default: other: desc:团队标题，团队不存在时必填，（团队存在即：team_id有值）
     * @param name:team_id type:int require:0 default: other: desc:团队id，团队存在时必填，（团队存在即：team_id有值）
     * @param name:name type:string require:1 default: other: desc:姓名
     * @param name:image type:int require:1 default: other: desc:照片
     * @param name:sex type:string require:1 default: other: desc:性别：男、女
     * @param name:age type:int require:1 default: other: desc:年龄
     * @param name:tel type:string require:1 default: other: desc:手机号
     * @param name:card type:string require:1 default: other: desc:身份证号码
     * @param name:unit type:string require:1 default: other: desc:工作单位
     * @param name:province type:string require:1 default: other: desc:省
     * @param name:city type:string require:1 default: other: desc:市
     * @param name:area type:string require:1 default: other: desc:区
     * @param name:address type:string require:1 default: other: desc:详细地址
     * @param name:status type:int require:0 default: other: desc:审核状态：0待审核，1通过审核，2拒绝
     * @param name:examine1 type:string require:1 default: other: desc:问题1：是、否
     * @param name:examine2 type:string require:1 default: other: desc:问题2：是、否
     * @param name:examine3 type:string require:1 default: other: desc:问题3：是、否
     * @param name:examine4 type:string require:1 default: other: desc:问题4：离开原因、否
     * @param name:accident_insurance type:string require:1 default: other: desc:是否接受大赛期间意外伤害保险：是、否
     * @param name:share_id type:string require:0 default:0 other: desc:分销员的id
     *
     *
     * @return id:返回报名id，支付需要使用
     * @return pay_state:支付状态：0未支付（需要请求支付接口），1已支付
     * @return amount:支付金额，单位元
     */
    public function create(){
        $data = input('post.');
        //
        $where = [
            ['id', '=', $data['match_id']],
            ['status', '=', DBCont::SUCCESS],
        ];
        $match = model('Match')->where($where)->find();;
        if (empty($match) || $match->isEmpty()){
            $this->responseFail('比赛未开始，不能报名');
        }
        $data['status'] = DBCont::WAIT;
        $data['wechat_id'] = getPersonId();
        if ($match['is_fees'] == DBCont::CHARGE) {
            $data['way'] = DBCont::WECHAT;
            $data['pay_state'] = DBCont::WAIT;
            $data['amount'] = $match['cost'];
            $data['status'] = DBCont::WAIT;
        } else {
            $data['way'] = DBCont::FREE;
            $data['pay_state'] = DBCont::SUCCESS;
            $data['amount'] = 0;
            $data['status'] = DBCont::SUCCESS;
        }
        if ($data['team_id']){
            $r = model('Member')->addMember($data, $data['team_id']);
            if ($r === false){
                $this->responseFail(model('Member')->getError());
            }
        } else {
            $r = model('Team')->addTeam($data);
            if ($r === false){
                $this->responseFail(model('Team')->getError());
            }
        }
        $data = model('Member')->where('id', $r['id'])->find();
        $this->responseSuccess($data, '提交成功');
    }
}